package com.lzxuni.modules.soilmoisture.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.soilmoisture.entity.TblSoilMoisture;
import com.lzxuni.modules.soilmoisture.service.TblSoilMoistureService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 墒情 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/LR_SoilMoisture/SoilMoisture")
public class TblSoilMoistureController extends BaseController {

    @Autowired
    private TblSoilMoistureService tblSoilMoistureService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_SoilMoisture/soilmoisturemanagement/index");
        return mv;
    }
    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        TblSoilMoisture tblSoilMoisture = JSON.parseObject(queryJson, TblSoilMoisture.class);
        PageData pageData = getPageData(tblSoilMoistureService.queryPage(pageParameter, tblSoilMoisture));
        return R.ok().put("data", pageData);
    }
    //导出
    @SysLog(categoryId = LogConstant.VIEWID,module = "墒情管理",operateType = LogConstant.EXPORT)
    @RequestMapping("/Export")
    public void exportExcel(HttpServletResponse response, String queryJson, HttpServletRequest request) throws IOException {
        TblSoilMoisture tblSoilMoisture = JSON.parseObject(queryJson, TblSoilMoisture.class);

        BufferedOutputStream fos = new BufferedOutputStream(response.getOutputStream());
        String filename = "墒情管理" + DateUtil.DateToString(new Date(), "yyyyMMddHHmm") + ".xls";
        String agent = request.getHeader("User-Agent");
        filename = FileUtil.encodeDownloadFilename(filename, agent);
        String contentType = request.getSession().getServletContext().getMimeType(filename);
        response.setContentType(contentType);
        response.setHeader("content-disposition", "attchment;filename=" + filename);
        tblSoilMoisture.setParentId(tblSoilMoisture.getTreeId());
        Workbook wb = null;
        try {
            wb = tblSoilMoistureService.exportExcel(tblSoilMoisture);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }
    }

    //墒情月分析页面
    @RequestMapping("/IndexMonth")
    public ModelAndView IndexMonth() {
        ModelAndView mv = new ModelAndView("/LR_SoilMoisture/soilmoisturemanagement/index_month");
        return mv;
    }

    //墒情月分析Echar图
    @RequestMapping("/DayEcharts")
    public Object getMothEcharts(TblSoilMoisture tblSoilMoisture) {
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
//        Date asd = new Date();
//        String shijian = sdf1.format(asd);
        List<TblSoilMoisture> soilMoisturelist = tblSoilMoistureService.queryMonthList(tblSoilMoisture);
        return R.ok().put("data", JSON.toJSONString(soilMoisturelist));
    }

    //墒情年分析页面
    @RequestMapping("/IndexYear")
    public ModelAndView IndexYear() {
        ModelAndView mv = new ModelAndView("/LR_SoilMoisture/soilmoisturemanagement/index_year");
        return mv;
    }
    //墒情年分析Echar图
    @RequestMapping("/YearEcharts")
    public Object getYearEcharts(TblSoilMoisture tblSoilMoisture) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        Date asd = new Date();
        String shijian = sdf1.format(asd);
        if(StringUtils.isNotEmpty(tblSoilMoisture.getSoilYear())){
            tblSoilMoisture.setSoilYear(tblSoilMoisture.getSoilYear());
        }else{
            tblSoilMoisture.setSoilYear(shijian);
        }
        List<TblSoilMoisture> soilMoisturelist = tblSoilMoistureService.queryYearList(tblSoilMoisture);
        return R.ok().put("data", JSON.toJSONString(soilMoisturelist));
    }

}

